package rhc.spider.util;

import java.util.Collection;

/**判断对象是否为空
 * @author lenovo
 *
 */
public class NullUtil {

	
	/**判断对象是否为null
	 * @param object
	 * @param throwsException
	 * @return
	 */
	public static boolean isNull(Object object,boolean throwsException){
		if(null == object){
			throwNullException(throwsException);
			return true;
		}
		return false;
	}
	
	/**判断String类型是否为空
	 * @param str
	 * @param throwsException
	 * @return
	 */
	public static boolean strIsNull(String str,boolean throwsException){
		if(null == str || str.trim().length()==0){
			throwNullException(throwsException);
			return true;
		}
		return false;
	}
	
	/**判断容器类型是否为空
	 * @param collection
	 * @param throwsException
	 * @return
	 */
	public static <T> boolean collectionIsNull(Collection<T> collection,boolean throwsException){
		if(null == collection || collection.isEmpty()){
			throwNullException(throwsException);
			return true;
		}
		return false;
	}
	
	/**数组是否为空
	 * @param arr
	 * @param throwsException
	 * @return
	 */
	public static <T> boolean arrayIsNull(T[] arr,boolean throwsException){
		if(null == arr || arr.length == 0){
			throwNullException(throwsException);
			return true;
		}
		return false;
	}
	
	private static void throwNullException(boolean throwsException){
		if(throwsException){
			throw new NullPointerException();
		}
	}
}
